package com.prp.bo;

import org.hibernate.Transaction;

import com.prp.db.DAOFactory;
import com.prp.db.HibernateSessionFactory;
import com.prp.db.idao.IAuthenticationDAO;
import com.prp.db.idao.IUserInformationDAO;
import com.prp.db.po.Authentication;
import com.prp.db.po.UserInformation;
import com.prp.util.mail.MD5;
import com.prp.util.mail.MailSender;

public class RegisterAuthMailSender {
	private static final String username = SNSConstants.MailSender_Username;
	private static final String password = SNSConstants.MailSender_Password;
	private static final String sendProtocol = SNSConstants.MailSender_SendProtocol;
	private static final String sendHost = SNSConstants.MailSender_SendHost;
	private static final String fromAddr = SNSConstants.MailSender_FromAddress;
	private static String toAddr = "";

	private static boolean send(final String toAddress, String content, String subject) {
		setToAddress(toAddress);
		return sendMail(content, subject);
	}

	private static void setToAddress(String toAddress)
			throws IllegalArgumentException {
		if (toAddress == null)
			throw new IllegalArgumentException("wrong recepient address: "
					+ toAddress);
		RegisterAuthMailSender.toAddr = toAddress;
	}

	private static boolean sendMail(String content, String subject) {
		MailSender mailSender = new MailSender(username, password, sendHost,
				sendProtocol, true);
		try {
			mailSender.sendMail(fromAddr, toAddr, content, subject);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public static void main(String[] args) {
		//sendRegisterConfirmMail("qiurenjie@msn.com");
		sendRetrievePasswordMail("qiurenjie@msn.com");
	}

	private static String generateAuthCode(String toAddress) {
		return MD5.encode(toAddress + System.currentTimeMillis());
	}

	public static void sendRegisterConfirmMail(String toAddress, String host) {
		String authCode = generateAuthCode(toAddress);
		String content = "感谢您的注册。<br>" + "<a href=\"" + host
				+ "registerActivate?autoCode="+authCode
				+ "\" target=\"_blank\">请点击此链接激活您的帐户</a><br/>" + "此邮件来自SNS-PRP";
		if (!send(toAddress, content, "请激活您的帐户"))
			return;
		DAOFactory factory = DAOFactory.getDAOFactory();
		IAuthenticationDAO authDAO = factory.getAuthenticationDAO();
		IUserInformationDAO userDAO = factory.getUserInformationDAO();
		Transaction tx = null;
		try {
			tx = HibernateSessionFactory.getSession().beginTransaction();
			Authentication auth = new Authentication();
			int id = ((UserInformation) userDAO.findByEmail(toAddress).get(0))
					.getUserId();
			auth.setUserId(id);
			auth.setAuthCode(authCode);
			authDAO.save(auth);
			tx.commit();
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			HibernateSessionFactory.getSession().close();
		}
	}

	public static void sendRetrievePasswordMail(String toAddress) {
		DAOFactory factory = DAOFactory.getDAOFactory();
		IUserInformationDAO userDAO = factory.getUserInformationDAO();
		UserInformation info = (UserInformation) userDAO.findByEmail(toAddress)
				.get(0);
		String content = "您的密码为:<br/>" + info.getPassword()
				+ "<br/>此邮件来自SNS-PRP";
		send(toAddress, content, "找回密码");
	}
}
